<?php

include_once('getDBConnection.php');
include_once('FavoriteFood.php');

$obj = new DBConnection();
$mysqli = $obj->getDBC();

//check user session validation

//create new Query
$query = '';
//assume post name is an array
if(isset($_POST['name'])){


	$food = new FavoriteFood();
	$food->id = (empty($_POST['id'])) ? '0' : $_POST['id'];//get the foods existing ID

	$food->name = (empty($_POST['name'])) ? 'Generic Food' : $mysqli->real_escape_string($_POST['name']);
	$food->calories = (empty($_POST['calories'])) ? '0' : $_POST['calories'];
	$food->fat = (empty($_POST['fat'])) ? '0' : $_POST['fat'];
	$food->fiber = (empty($_POST['fiber'])) ? '0' : $_POST['fiber'];

	$food->points = (empty($_POST['points'])) ? '0' : $_POST['points'];
	$food->servingSize = (empty($_POST['servingSize'])) ? '1' : $_POST['servingSize'];

	//$food->isFavorites = (empty($_POST['isFavorites'])) ? 'false' : $_POST['isFavorites'];
	//$food->isQuickAdd = (empty($_POST['isQuickAdd'])) ? 'false' : $_POST['isQuickAdd'];

//	$food->mealTime = (empty($_POST['mealTime'])) ? 'morning' : $_POST['mealTime'];
	$food->dateCreated = (empty($_POST['dateCreated'])) ? 'NOW()' : $_POST['dateCreated'];
	$food->dateModified = (empty($_POST['dateModified'])) ? 'NOW()' : $_POST['dateModified'];

	$food->createdBy = (empty($_POST['createdBy'])) ? 'defaultUser' : $_POST['createdBy'];
	$food->type = (empty($_POST['type'])) ? 'Food' : $_POST['type'];


	$test = 'select count(*) from food where id = ' . $food->id . ';';
	$count = $mysqli->query($test);

	if($mysqli->error){
		die('An error occurred while trying to determine if a duplicate item existed in the database because ' . $mysqli->error);
	}

	if($count){
		$count = $count->fetch_row();//turn count from a result set to the first row I.E. the number of rows stored in a 1 index array
	}else{
		die('unable to fetch count for from database');
	}

	if($count[0] == 0){//if a result is returned and the row returned 0 (note this is a bit redundant because this should always return the number of rows or 0;
		$query = "insert into " . Constants::FOOD_TABLE . " values ( " .
			"$food->id, " .//id
			"'$food->name', " .//name is a string
			"'$food->type', " .//type is a string
			"$food->calories, " .//calories
			"$food->fat, " .//fat
			"$food->fiber, " .//fiber
			"$food->points, " .//points
			"$food->servingSize, " .//serving size
			"$food->dateCreated, " .//date created
			"$food->dateModified, " .//date modified
			"'$food->createdBy')";//is string

	}else{//update existing food
		$query = 'update ' . Constants::FOOD_TABLE . ' set ' .
			"$food->id, " .//id
			"'$food->name', " .//name is a string
			"'$food->type', " .//type is a string
			"$food->calories, " .//calories
			"$food->fat, " .//fat
			"$food->fiber, " .//fiber
			"$food->points, " .//points
			"$food->servingSize, " .//serving size
			"$food->dateModified, " .//date modified
			"where id = $food->id";
	}
	$result = $mysqli->query($query);

	if($mysqli->error){
		die('An error occurred while trying to save the food to the database: ' . $mysqli->error);
	}

}else{
	$result = 'item passed to the database was not correctly set';
	return($result);
}
$mysqli->close();

?>